package cn.itsource.service.impl;

import cn.itsource.annotation.Logs;
import cn.itsource.domain.Article;
import cn.itsource.domain.Param;
import cn.itsource.mapper.ParamMapper;
import cn.itsource.query.ParamQuery;
import cn.itsource.service.IParamService;
import cn.itsource.vo.ArticleCategory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ParamServiceImpl implements IParamService {
    @Autowired
    private ParamMapper paramMapper;
    @Logs
    @Transactional
    @Override
    @CacheEvict(cacheNames = "param", key = "'typeTree'")
    public void add(Param param) {
        paramMapper.add(param);
    }
    @Logs
    @Transactional
    @Override
    @CacheEvict(cacheNames = "param", key = "'typeTree'")
    public void update(Param param) {
        param.setUpdateTime(new Date());
        paramMapper.update(param);
    }
    @Logs
    @Transactional
    @Override
    @CacheEvict(cacheNames = "param", key = "'typeTree'")
    public void del(Long id) {
        paramMapper.del(id);
    }
    @Logs
    @Transactional
    @Override
    public void delList(List<Long> ids) {
        paramMapper.delList(ids);
    }

    @Override
    public Param get(Long id) {
        return paramMapper.get(id);
    }

    @Override
    public Map<String,Object> getAll() {
        List<Param> params = paramMapper.getAll();
        HashMap<String, Object> map = new HashMap<>();
        for (Param param : params) {
            map.put(param.getParamKey(),param.getParamValue());
        }
        return map;
    }

    @Override
    public PageInfo<Param> pageQuery(ParamQuery query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Param> param = paramMapper.pageQuery(query);
        return new PageInfo<>(param);
    }

}
